package main

import "math"

func main() {

}

func sortedSquares(nums []int) []int {
	if nums[len(nums)-1] <= 0 {
		for i := 0; i < len(nums); i++ {
			nums[i], nums[len(nums)-i-1] = nums[len(nums)-i-1]*nums[len(nums)-1-i], nums[i]*nums[i]
		}
		return nums
	}
	if nums[0] >= 0 {
		for i := 0; i < len(nums); i++ {
			nums[i] = nums[i] * nums[i]
		}
		return nums
	}
	ans := make([]int, 0, len(nums))
	for i, j := 0, len(nums)-1; i <= j; {
		if int(math.Abs(float64(nums[i]))) <= int(math.Abs(float64(nums[i]))) {
			ans[j-i] = nums[i] * nums[i]
			i++
		} else {
			ans[j-i] = nums[j] * nums[j]
			j--
		}
	}
	return ans
}
